package sil;

import iface.ChartInfo;
import iface.ExtractChartInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import container.ProbabilityContainer;

/**
 * Helper class to extract probability information for graphical display uses.
 * 
 * @author Joana Otetelisanu
 * @author Marcus Lindner
 */
public enum ChartInfoExtractor implements ExtractChartInfo {
    /** Extracts the MoS information.*/
    MOS {
	@Override
	public ChartInfo extractValues(ProbabilityContainer container) {
	    return extractInfo(container.getMosProbabilities());

	}
    },
    /** Extracts the Damage information.*/
    DAMAGE {
	@Override
	public ChartInfo extractValues(ProbabilityContainer container) {
	    return extractInfo(container.getDamageProbabilities());
	}
    };

    /**
     * @param possibilityMap
     * @return
     */
    protected ChartInfo extractInfo(Map<Integer, Number> possibilityMap) {
	final double[] val = new double[possibilityMap.size()];
	final String[] names = new String[possibilityMap.size()];

	List<Integer> keys = new ArrayList<Integer>(possibilityMap.keySet());
	Collections.sort(keys);

	int i = 0;
	for (Integer key : keys) {
	    Number value = possibilityMap.get(key);
	    
	    val[i] = value.doubleValue();

	    names[i] = key.toString();
	    i++;
	}

	return new ChartInfo() {

	    @Override
	    public double[] getValues() {
		return val;
	    }

	    @Override
	    public String[] getNames() {
		return names;
	    }
	};
    }
}
